//
//  ViewController.m
//  CAShaperLayer
//
//  Created by BAP_0006 on 2018/3/30.
//  Copyright © 2018年 BAP_0006. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    dispatch_queue_t queue;
    dispatch_source_t timer;
}
@property (nonatomic, strong)UIButton *btn;

@property (nonatomic, strong)CAShapeLayer *layer;

@property (nonatomic, strong)CABasicAnimation *pathAnimation;

@property (nonatomic, strong)NSTimer *timer;

@property (nonatomic, assign)float t;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.t = 0.0;
    CGRect ScreenRect= [[UIScreen mainScreen] bounds];
    // 创建一个view
    UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(ScreenRect.size.width / 2.0 - 100, ScreenRect.size.height / 2.0 - 100, 200, 200)];
    [self.view addSubview:showView];
    
    // 贝塞尔曲线(创建一个圆)
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(200 / 2.f, 200 / 2.f)
                                                        radius:200 / 2.f
                                                    startAngle:0
                                                      endAngle:M_PI * 2
                                                     clockwise:YES];
    
    // 创建一个shapeLayer
    _layer = [CAShapeLayer layer];
    _layer.frame         = showView.bounds;                // 与showView的frame一致
    _layer.strokeColor   = [UIColor greenColor].CGColor;   // 边缘线的颜色
    _layer.fillColor     = [UIColor clearColor].CGColor;   // 闭环填充的颜色
    _layer.lineCap       = kCALineCapRound;               // 边缘线的类型
    _layer.path          = path.CGPath;                    // 从贝塞尔曲线获取到形状
    _layer.lineWidth     = 6.0f;                           // 线条宽度
    _layer.strokeStart   = 0.0f;
    _layer.strokeEnd     = 0.0f;
    
    // 将layer添加进图层
    [showView.layer addSublayer:_layer];

    // 给这个layer添加动画效果
    _pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    _pathAnimation.duration = 5.0;
    _pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    _pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
    _pathAnimation.repeatCount = 1;
    
    
    self.btn = [[UIButton alloc]initWithFrame:CGRectMake(ScreenRect.size.width / 2.0 - 100, ScreenRect.size.height / 2.0 - 100, 200, 200)];
    [self.btn setTitle:@"开始" forState:0];
    [self.btn setTitleColor:[UIColor blackColor] forState:0];
    self.btn.titleLabel.font = [UIFont systemFontOfSize:84];
    [self.btn addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btn];
    //GCD方式
    uint64_t interval = 0.1 * NSEC_PER_SEC;
    //创建一个专门执行timer回调的GCD队列
    queue = dispatch_queue_create("timerQueue", 0);
    timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    //使用dispatch_source_set_timer函数设置timer参数
    dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, 0), interval, 0.1);
    //设置回调
    __weak typeof(self) weakSelf = self;
    dispatch_source_set_event_handler(timer, ^(){
        weakSelf.t += 0.1;
        dispatch_async(dispatch_get_main_queue(), ^{
            // UI更新代码
            [_btn setTitle:[NSString stringWithFormat:@"%.1f",weakSelf.t] forState:0];
        });
        
        NSLog(@"Timer %@", [NSThread currentThread]);
    });
    
}

- (void)start{
    _layer.strokeEnd     = 1.0f;
    [_layer addAnimation:_pathAnimation forKey:nil];
    dispatch_resume(timer);//dispatch_source默认是Suspended状态，通过dispatch_resume函数开始它
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
